﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WarehouseManagement.BusinessLogic.BC;
using WarehouseManagement.ViewModels;
using WarehouseManagement.ViewModels.Merchandise;
using WarehouseManagement.Views.Merchandise;

namespace WarehouseManagement.Presenters.Merchandise
{
    public class MerchandiseListPresenter : BasePresenter<IMerchandiseListViewModel>
    {
        private MerchandiseBC _bc;

        public MerchandiseListPresenter()
        {
            _bc = new MerchandiseBC();
        }

        protected override void WireupEventHandles()
        {

        }

        public void LoadMerchandiseList()
        {
            Model.Merchandises = _bc.GetMerchandiseDTOList();
        }

        public void AddMerchandise()
        {
            NewEditMerchandise frm = new NewEditMerchandise();
            frm.OnMerchandiseSaved = new NewEditMerchandise.MerchandiseSavedHandler(MerchandiseSaved);
            frm.Text = "Thêm mặt hàng";
            frm.IsEdit = false;
            frm.ShowDialog();
        }

        public void EditMerchandise()
        {
            NewEditMerchandise frm = new NewEditMerchandise();
            frm.OnMerchandiseSaved = new NewEditMerchandise.MerchandiseSavedHandler(MerchandiseSaved);
            frm.IsEdit = true;
            frm.Text = "Sửa mặt hàng";

            frm.Id = Model.SelectedMerchandise.Id;
            frm.MerchandiseName = Model.SelectedMerchandise.MerchandiseName;
            frm.Unit = Model.SelectedMerchandise.Unit;
            frm.TradeMark = Model.SelectedMerchandise.TradeMark;
            frm.Madein = Model.SelectedMerchandise.Madein;
            frm.CategoryId = Model.SelectedMerchandise.CategoryId;

            frm.ShowDialog();
        }
        private void MerchandiseSaved(bool success, bool isEdit)
        {
            if (success)
            {
                LoadMerchandiseList();
            }
            else
            {
                string msg;
                if (isEdit)
                    msg = "Không thể cập nhật mặt hàng";
                else
                    msg = "Không thể thêm mặt hàng";
                XtraMessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void DeleteMerchandises()
        {
            var selectedItem = Model.SelectedMerchandise;

            if (XtraMessageBox.Show("Bạn có chắc muốn xóa những mặt hàng đã chọn", "Xóa mặt hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;


            try
            {
                _bc.DeleteMerchandises(new[] { selectedItem.Id });
                Model.LoadGridData();
            }
            catch
            {
                XtraMessageBox.Show("Không thể xóa các mặt hàng đã chọn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                _bc.RecycleContext();
              
            }

        }


    }
}
